package com.zjj.bams.entity;

import com.zjj.bams.exception.BalanceNotEnoughException;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
public class CreditAccount extends Account{

    //新的成员属性  透支额度
    private double ceiling;

    public double getCeiling() {
        return ceiling;
    }

    public void setCeiling(double ceiling) {
        this.ceiling = ceiling;
    }

    public CreditAccount() {

    }

    public CreditAccount(String password, String name, String personId, String email, double balance, double ceiling) {
        super(password, name, personId, email, balance);
        this.ceiling = ceiling;
    }

    public CreditAccount(Long id, String password, String name, String personId, String email, double balance, double ceiling) {
        super(id, password, name, personId, email, balance);
        this.ceiling = ceiling;
    }

    /**
     * 重写  取款逻辑
     * 情况1：balance 1000  ceiling 500  money 800  ->  balance=1000-800=200
     * 情况2：balance 500   ceiling 500  money 800  ->  balance=0.0  ceiling=ceiling-(money-balance)
     * 情况3：balance 200   ceiling 500  money 800  ->  balance=200  ceiling=500  余额不足
     *
     * @param money  钱
     * @return
     */
    @Override
    public Account withdraw(double money) throws BalanceNotEnoughException {
        if(this.getBalance()+ceiling>=money){
            if(this.getBalance()>=money){
                this.setBalance(this.getBalance() - money);
            }else{
                this.ceiling-=money-this.getBalance();
                this.setBalance(0.0);
            }
            return this;
        }
        throw new BalanceNotEnoughException("您的银行卡余额不足");
        /*System.out.println("您的银行卡余额不足");
        return this;*/
    }

    @Override
    public String toString() {
        return "CreditAccount{" +
                "ceiling=" + ceiling +
                "} " + super.toString();
    }
}
